package cn.ldf.keepaccounts.controller;


import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.ldf.keepaccounts.entity.pojo.Members;
import cn.ldf.keepaccounts.entity.vo.MembersAvatarVO;
import cn.ldf.keepaccounts.entity.vo.R;
import cn.ldf.keepaccounts.service.MembersService;
import cn.ldf.keepaccounts.utils.ExceptionUtil;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 房间成员表(Members)表控制层
 *
 * @author 清峰
 * @since 2025-02-17 10:12:34
 */
@RestController
@RequestMapping("members")
public class MembersController {
    /**
     * 服务对象
     */
    @Resource
    private MembersService membersService;


    /**
     * 添加房间成员
     *
     * @param members 房间成员
     * @return 结果集
     */
    @PostMapping("/addMembers")
    public R<Members> addMembers(@Valid @RequestBody Members members) {
        ExceptionUtil.throwMsg(() -> ObjUtil.isEmpty(members.getRoomId()), "房间id不能为空");
        return R.success(membersService.addMembers(members));
    }
    /**
     * 修改房间成员昵称
     *
     * @param members 房间成员
     * @return 房间成员头像
     */
    @PostMapping("/updateMembersNickname")
    public R<Members> updateMembersNickname(@RequestBody Members members) {
        ExceptionUtil.throwMsg(() -> ObjUtil.isEmpty(members.getRoomId()), "房间id不能为空");
        ExceptionUtil.throwMsg(() -> ObjUtil.isEmpty(members.getOpenId()), "成员openId不能为空");
        ExceptionUtil.throwMsg(() -> StrUtil.isBlank(members.getNickname()), "成员昵称不能为空");
        return R.success(membersService.updateMembersNickname(members));
    }
    /**
     * 根据房间id查询未结算的房间成员
     *
     * @param roomId 房间id
     * @return 房间成员
     */
    @GetMapping("/getNoSettledByRoomId/{roomId}")
    public R<List<Members>> getNoSettledByRoomId(@PathVariable String roomId) {
        return R.success(membersService.getNoSettledByRoomId(roomId));
    }
    /**
     * 根据房间id查询房间内所有成员信息(不含头像)
     *
     * @param roomId 房间id
     * @return 房间成员
     */
    @GetMapping("/getAllByRoomId/{roomId}")
    public R<List<Members>> getAllByRoomId(@PathVariable String roomId) {
        return R.success(membersService.getAllByRoomId(roomId));
    }

    /**
     * 根据房间id查询房间内所有成员信息(含头像)
     *
     * @param roomId 房间id
     * @return 房间成员头像
     */
    @GetMapping("/getAvatarByRoomId/{roomId}")
    public R<List<MembersAvatarVO>> getAvatarByRoomId(@PathVariable String roomId) {
        return R.success(membersService.getAvatarByRoomId(roomId));
    }

    /**
     * 根据房间id查询房间内所有是否赢家的成员信息(含头像)
     *
     * @param roomId 房间id
     * @return 房间成员头像
     */
    @GetMapping("/getListMembersByIsWinn")
    public R<List<MembersAvatarVO>> getListMembersByIsWinn(@RequestParam String roomId,
                                                           @RequestParam String currentOpenId) {

        return R.success(membersService.getListMembersByIsWinn(roomId,currentOpenId));
    }

}

